/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IPPanel.java
 *
 * Created on 2010-01-14, 17:51:09
 */

package efe.gui;

import efe.bean.*;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.Vector;

/**
 * Panel z nagłówkiem protokołu IP.
 * @author Paweł
 */
public class IPPanel extends javax.swing.JPanel implements Serializable {

    /** Creates new form IPPanel */
    public IPPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        TOSDialog = new javax.swing.JDialog();
        precedenceFiled = new efe.bean.EfeEditorField();
        delayField = new efe.bean.EfeEditorField();
        throughputField = new efe.bean.EfeEditorField();
        reliabilityField = new efe.bean.EfeEditorField();
        reservedTOSField = new efe.bean.EfeEditorField();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        precedenceValue = new efe.bean.EfeNumberValue();
        delayValue = new efe.bean.EfeBooleanValue();
        throughputValue = new efe.bean.EfeBooleanValue();
        reliabilityValue = new efe.bean.EfeBooleanValue();
        reservedTOSValue = new efe.bean.EfeNumberValue();
        identyficationValue = new efe.bean.Efe16bitNumberValue();
        FlagsDialog = new javax.swing.JDialog();
        jPanel10 = new javax.swing.JPanel();
        reservedFlagField = new efe.bean.EfeEditorField();
        dontFragmentField = new efe.bean.EfeEditorField();
        moreFragmentsField = new efe.bean.EfeEditorField();
        jPanel11 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        reservedFlagFieldValue = new efe.bean.EfeBooleanValue();
        dontFragmentValue = new efe.bean.EfeBooleanValue();
        moreFragmentsValue = new efe.bean.EfeBooleanValue();
        fragmentOffsetValue = new efe.bean.EfeNumberValue();
        ttlValue = new efe.bean.EfeNumberValue();
        sourceAddressValue = new efe.bean.EfeIPValue();
        destinationAddressValue = new efe.bean.EfeIPValue();
        jPanel4 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        versionField = new efe.bean.EfeEditorBlankField();
        headerLengthField = new efe.bean.EfeEditorBlankField();
        typeOfServiceField = new efe.bean.EfeEditorExpandField();
        jPanel5 = new javax.swing.JPanel();
        efeEditorBlankField2 = new efe.bean.EfeEditorBlankField();
        jPanel6 = new javax.swing.JPanel();
        identyficationField = new efe.bean.EfeEditorField();
        jPanel7 = new javax.swing.JPanel();
        flagsField = new efe.bean.EfeEditorExpandField();
        fragmentOffsetField = new efe.bean.EfeEditorField();
        jPanel8 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        ttlField = new efe.bean.EfeEditorField();
        efeEditorBlankField3 = new efe.bean.EfeEditorBlankField();
        efeEditorBlankField1 = new efe.bean.EfeEditorBlankField();
        sourceAddressField = new efe.bean.EfeEditorField();
        destinationAddressField = new efe.bean.EfeEditorField();
        efeEditorBlankField4 = new efe.bean.EfeEditorBlankField();

        TOSDialog.setTitle("TOS");
        TOSDialog.setMinimumSize(new java.awt.Dimension(600, 100));
        TOSDialog.setResizable(false);
        TOSDialog.getContentPane().setLayout(new java.awt.GridBagLayout());

        precedenceFiled.setLabelText("Precedence");
        precedenceFiled.setValue(precedenceValue);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 40;
        gridBagConstraints.weightx = 3.0;
        TOSDialog.getContentPane().add(precedenceFiled, gridBagConstraints);

        delayField.setLabelText("D");
        delayField.setValue(delayValue);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 40;
        gridBagConstraints.weightx = 1.0;
        TOSDialog.getContentPane().add(delayField, gridBagConstraints);

        throughputField.setLabelText("T");
        throughputField.setValue(throughputValue);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 40;
        gridBagConstraints.weightx = 1.0;
        TOSDialog.getContentPane().add(throughputField, gridBagConstraints);

        reliabilityField.setLabelText("R");
        reliabilityField.setValue(reliabilityValue);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 40;
        gridBagConstraints.weightx = 1.0;
        TOSDialog.getContentPane().add(reliabilityField, gridBagConstraints);

        reservedTOSField.setLabelText("Reserved");
        reservedTOSField.setValue(reservedTOSValue);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipady = 40;
        gridBagConstraints.weightx = 2.0;
        TOSDialog.getContentPane().add(reservedTOSField, gridBagConstraints);

        jLabel1.setText("D - delay");

        jLabel3.setText("T -  throughput");

        jLabel2.setText("R - reliability");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addContainerGap(104, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addContainerGap())
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 8;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        TOSDialog.getContentPane().add(jPanel3, gridBagConstraints);

        precedenceValue.setLimit(7);

        reservedTOSValue.setLimit(2);

        FlagsDialog.setTitle("IP Flags");
        FlagsDialog.setMinimumSize(new java.awt.Dimension(300, 200));
        FlagsDialog.getContentPane().setLayout(new java.awt.GridLayout(2, 1));

        jPanel10.setLayout(new java.awt.GridLayout(1, 0));

        reservedFlagField.setLabelText("Rsv");
        reservedFlagField.setValue(reservedFlagFieldValue);
        jPanel10.add(reservedFlagField);

        dontFragmentField.setLabelText("DF");
        dontFragmentField.setValue(dontFragmentValue);
        jPanel10.add(dontFragmentField);

        moreFragmentsField.setLabelText("MF");
        moreFragmentsField.setValue(moreFragmentsValue);
        jPanel10.add(moreFragmentsField);

        FlagsDialog.getContentPane().add(jPanel10);

        jLabel4.setText("Rsv - reserved");

        jLabel5.setText("DF - don't fragment");

        jLabel6.setText("MF - more fragments");

        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addContainerGap(257, Short.MAX_VALUE))
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        FlagsDialog.getContentPane().add(jPanel11);

        fragmentOffsetValue.setLimit(16383);

        ttlValue.setNumber(100);
        ttlValue.setLimit(255);

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setPreferredSize(new java.awt.Dimension(620, 164));
        setLayout(new java.awt.GridLayout(6, 1));

        jPanel4.setLayout(new java.awt.GridLayout(1, 2));

        jPanel1.setLayout(new java.awt.GridLayout(1, 2));

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        versionField.setText("Version");
        jPanel2.add(versionField);

        headerLengthField.setText("Header Length (IHL)");
        jPanel2.add(headerLengthField);

        jPanel1.add(jPanel2);

        typeOfServiceField.setText("Type of service (TOS)");
        typeOfServiceField.setjDialog(TOSDialog);
        typeOfServiceField.setjDialog(TOSDialog);
        jPanel1.add(typeOfServiceField);

        jPanel4.add(jPanel1);

        jPanel5.setLayout(new java.awt.GridLayout(1, 1));

        efeEditorBlankField2.setText("Total length (TL)");
        jPanel5.add(efeEditorBlankField2);

        jPanel4.add(jPanel5);

        add(jPanel4);

        jPanel6.setLayout(new java.awt.GridLayout(1, 0));

        identyficationField.setLabelText("Identification");
        identyficationField.setValue(identyficationValue);
        identyficationField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                identyficationFieldActionPerformed(evt);
            }
        });
        jPanel6.add(identyficationField);

        flagsField.setText("Flags");
        flagsField.setjDialog(FlagsDialog);

        fragmentOffsetField.setLabelText("Fragment offset");
        fragmentOffsetField.setValue(fragmentOffsetValue);

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(flagsField, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(fragmentOffsetField, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(flagsField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(fragmentOffsetField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jPanel6.add(jPanel7);

        add(jPanel6);

        jPanel8.setLayout(new java.awt.GridLayout(1, 0));

        jPanel9.setLayout(new java.awt.GridLayout(1, 0));

        ttlField.setLabelText("Time to life (TTL)");
        ttlField.setValue(ttlValue);
        jPanel9.add(ttlField);

        efeEditorBlankField3.setText("Protocol");
        jPanel9.add(efeEditorBlankField3);

        jPanel8.add(jPanel9);

        efeEditorBlankField1.setText("Header checksum");
        jPanel8.add(efeEditorBlankField1);

        add(jPanel8);

        sourceAddressField.setLabelText("Source address");
        sourceAddressField.setValue(sourceAddressValue);
        add(sourceAddressField);

        destinationAddressField.setLabelText("Destination address");
        destinationAddressField.setValue(destinationAddressValue);
        add(destinationAddressField);

        efeEditorBlankField4.setText("Options");
        add(efeEditorBlankField4);
    }// </editor-fold>//GEN-END:initComponents

    private void identyficationFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_identyficationFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_identyficationFieldActionPerformed

    /**
     * Ustawia nagłówek IP dla danego segmentu.
     * @param packet segment w którym ustawia nagłówek
     * @param protocolNumber numer protokołu warstwy transportowej w polu
     * Protocol number nagłówka IP
     * @return pakiet z dołączonym nagłówkiem
     * @throws UnknownHostException
     */
    public jpcap.packet.IPPacket setIPHeader(jpcap.packet.IPPacket packet, short protocolNumber) throws UnknownHostException  {
        //setIPv4Parameter(int priority, boolean d_flag,
        //   boolean t_flag, boolean r_flag,
        //   int rsv_tos, boolean rsv_frag,
        //   boolean dont_frag, boolean more_frag,
        //   int offset, int ident,
        //   int ttl, int protocol,
        //   java.net.InetAddress src,
        //   java.net.InetAddress dst)
        packet.setIPv4Parameter(precedenceValue.getValue(), delayValue.getValue(),
                                throughputValue.getValue(), reliabilityValue.getValue(),
                                reservedTOSValue.getValue(), reservedFlagFieldValue.getValue(),
                                dontFragmentValue.getValue(), moreFragmentsValue.getValue(),
                                fragmentOffsetValue.getValue(), identyficationValue.getValue(),
                                ttlValue.getValue(), protocolNumber,
                                InetAddress.getByName(sourceAddressValue.getValue()),
                                InetAddress.getByName(destinationAddressValue.getValue()));
        packet.ident = identyficationValue.getValue();
        packet.offset = (short) fragmentOffsetValue.getValue(); // 0: fragmentacja > 0: bez fragmentacji
                                                        // wielokorotnosc 2048
        return packet;
    }

    /**
     * Zwraca wektor serializowalnych wartości, które należy zapisać do pliku.
     * @return kolekcja wartości nagłówka
     */
    public Vector<Object> getValuesVector() {
        Vector<Object> v = new Vector<Object>();
        v.add(delayValue);
        v.add(destinationAddressValue);
        v.add(dontFragmentValue);
        v.add(fragmentOffsetValue);
        v.add(identyficationValue);
        v.add(moreFragmentsValue);
        v.add(precedenceValue);
        v.add(reliabilityValue);
        v.add(reservedFlagFieldValue);
        v.add(reservedTOSValue);
        v.add(sourceAddressValue);
        v.add(throughputValue);
        v.add(ttlValue);
        return v;
    }

    /**
     * Zapisuje dane z wektora do pól nagłówka.
     * @param v wektor wartości z elementami nagłówka w kolejności takiej samej
     * jak ta zwrócona przez metodę getValuesVector()
     */
    public void setValuesFromVector(Vector<Object> v) {
        Iterator<Object> i = v.iterator();
        delayValue = (EfeBooleanValue) i.next();
        delayField.setValue(delayValue);
        destinationAddressValue = (EfeIPValue) i.next();
        destinationAddressField.setValue(destinationAddressValue);
        dontFragmentValue = (EfeBooleanValue) i.next();
        dontFragmentField.setValue(dontFragmentValue);
        fragmentOffsetValue = (EfeNumberValue) i.next();
        fragmentOffsetField.setValue(fragmentOffsetValue);
        identyficationValue = (Efe16bitNumberValue) i.next();
        identyficationField.setValue(identyficationValue);
        moreFragmentsValue = (EfeBooleanValue) i.next();
        moreFragmentsField.setValue(moreFragmentsValue);
        precedenceValue = (EfeNumberValue) i.next();
        precedenceFiled.setValue(precedenceValue);
        reliabilityValue = (EfeBooleanValue) i.next();
        reliabilityField.setValue(reliabilityValue);
        reservedFlagFieldValue = (EfeBooleanValue) i.next();
        reservedFlagField.setValue(reservedFlagFieldValue);
        reservedTOSValue = (EfeNumberValue) i.next();
        reservedTOSField.setValue(reservedTOSValue);
        sourceAddressValue = (EfeIPValue) i.next();
        sourceAddressField.setValue(sourceAddressValue);
        throughputValue = (EfeBooleanValue) i.next();
        throughputField.setValue(throughputValue);
        ttlValue = (EfeNumberValue) i.next();
        ttlField.setValue(ttlValue);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog FlagsDialog;
    private javax.swing.JDialog TOSDialog;
    private efe.bean.EfeEditorField delayField;
    private efe.bean.EfeBooleanValue delayValue;
    private efe.bean.EfeEditorField destinationAddressField;
    private efe.bean.EfeIPValue destinationAddressValue;
    private efe.bean.EfeEditorField dontFragmentField;
    private efe.bean.EfeBooleanValue dontFragmentValue;
    private efe.bean.EfeEditorBlankField efeEditorBlankField1;
    private efe.bean.EfeEditorBlankField efeEditorBlankField2;
    private efe.bean.EfeEditorBlankField efeEditorBlankField3;
    private efe.bean.EfeEditorBlankField efeEditorBlankField4;
    private efe.bean.EfeEditorExpandField flagsField;
    private efe.bean.EfeEditorField fragmentOffsetField;
    private efe.bean.EfeNumberValue fragmentOffsetValue;
    private efe.bean.EfeEditorBlankField headerLengthField;
    private efe.bean.EfeEditorField identyficationField;
    private efe.bean.Efe16bitNumberValue identyficationValue;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private efe.bean.EfeEditorField moreFragmentsField;
    private efe.bean.EfeBooleanValue moreFragmentsValue;
    private efe.bean.EfeEditorField precedenceFiled;
    private efe.bean.EfeNumberValue precedenceValue;
    private efe.bean.EfeEditorField reliabilityField;
    private efe.bean.EfeBooleanValue reliabilityValue;
    private efe.bean.EfeEditorField reservedFlagField;
    private efe.bean.EfeBooleanValue reservedFlagFieldValue;
    private efe.bean.EfeEditorField reservedTOSField;
    private efe.bean.EfeNumberValue reservedTOSValue;
    private efe.bean.EfeEditorField sourceAddressField;
    private efe.bean.EfeIPValue sourceAddressValue;
    private efe.bean.EfeEditorField throughputField;
    private efe.bean.EfeBooleanValue throughputValue;
    private efe.bean.EfeEditorField ttlField;
    private efe.bean.EfeNumberValue ttlValue;
    private efe.bean.EfeEditorExpandField typeOfServiceField;
    private efe.bean.EfeEditorBlankField versionField;
    // End of variables declaration//GEN-END:variables

}
